import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
// import { usePermissStore } from '@/store/permiss';
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { CommodityRoute, FILE_ROUTE, NOTE_ROUTE, SiteCollection, USER } from './routes/app';
const LAYOUT = (() => import('@/layout/layout.vue'))
import storage from '@/utils/storage';

const routes: RouteRecordRaw[] = [
    {
        path: '/',
        redirect: '/user',
    },
    {
        path: '/',
        name: 'LAYOUT',
        component: LAYOUT,
        children: [
            {
                path: 'dashboard',
                name: 'Dashboard',
                meta: {
                    title: '系统首页',
                    permiss: '1',
                },
                component: () => import(/* webpackChunkName: "dashboard" */ '../views/example/dashboard.vue'),
            },
            NOTE_ROUTE,
            FILE_ROUTE,
            USER,
        ]
    },
    SiteCollection,
    CommodityRoute,
    {
        path: '/login',
        name: 'Login',
        meta: {
            title: '登录',
        },
        component: () => import(/* webpackChunkName: "login" */ '../views/example/login.vue'),
    },
    {
        path: '/403',
        name: '403',
        meta: {
            title: '没有权限',
        },
        component: () => import(/* webpackChunkName: "403" */ '../views/example/403.vue'),
    }
];

const router = createRouter({
    history: createWebHashHistory(),
    routes,
});

router.beforeEach((to, from, next) => {
    NProgress.start();
    const role = storage.getItem('token');
    
    // const permiss = usePermissStore();
    
    if (!role && to.path !== '/login') {
        next('/login');
    } else {
        next();
    }
});

router.afterEach(() => {
    NProgress.done()
})

export default router;
